<?php
require_once('./admin/connexion.php');
require ('./func.req.php');
if (mysql_num_rows($result_info) == 0) {
    ?>
    <link rel="stylesheet" type="text/css" href="./css/style.css" title="" media="all" />
    <div id="box" onclick="document.getElementById('box').style.display='none';" style="margin:20%;" class="error-box rounded" title="Fermer la fen&ecirc;tre">
        L'installation n'a pas encore &eacute;t&eacute; r&eacute;alis&eacute;e. <a href="./install/index.php">Cliquez ici</a> pour commencer.
    </div>
    <?php
} else if ($line_info['maintenance_choix_info'] == "Oui") {
    show_maintenance();
} else {

    /*
      -----------------------------------
      ---- SCRIPT D'AUTHENTIFICATION ----
      FujiCraft V3.0
      By Fujido
      Contact : thefujido@gmail.com
      -----------------------------------
     */

    session_start(); // On relaye la session
    if (@session_is_registered("authentification")) { // v�rification sur la session authentification (la session est elle enregistr�e ?)
// ici les �ventuelles actions en cas de r�ussite de la connexion
    } else {
        header("Location:login.php?erreur=needlog"); // redirection en cas d'echec
    }

// GESTION DE LA D�connexion
    if (isset($_GET['erreur']) && $_GET['erreur'] == 'logout') { // Test sur les param�tres d'URL qui permettront d'identifier un "contexte" de d�connexion
        $prenom = $_SESSION['prenom']; // On garde le pr�nom en variable pour dire au revoir (soyons polis :-)
        @session_unset("authentification");
        header("Location:login.php?erreur=logout");
    }
    ?>
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
        <head>
            <title><?php echo $site; ?> | Bo&icirc;te de r&eacute;ception de <?php echo $_SESSION['login']; ?></title>
            <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
            <meta http-equiv="Content-Style-Type" content="text/css" />
            <meta http-equiv="content-language" content="fr,en" />
            <link rel='shortcut icon' type="image/x-icon" href='./images/favicon.ico' />
            <link rel="stylesheet" type="text/css" href="./css/style.css" title="" media="all" />
            <link rel="stylesheet" type="text/css" href="./css/register.css" title="" media="all" />
            <link rel="stylesheet" type="text/css" href="./css/news.css" title="" media="all" />
            <script type="text/javascript" src="./js/news.js"></script>
        </head>
        <body>
            <div id="block">
                <div id="contentblock">
                    <div class="wrapper">
                        <?php
                        show_header();
                        show_menu();
                        show_newsbox();
                        show_userbox();
                        ?>
                        <div class="mainbox">
                            <h2>Bo&icirc;te de r&eacute;ception de "<i><a href="./user.php?user=<?php echo $_SESSION['login']; ?>" title="Voir le profil de <?php echo $_SESSION['login']; ?>"><?php echo $_SESSION['login']; ?></a></i>"</h2>
                            <br />
                            <center>
                                <?php
                                $login = $_SESSION['login'];
                                //Selection de la table
                                $query_usr = "SELECT * FROM " . $database . "." . $tb_mp . " WHERE pseudo_mp = '$login' OR from_mp = '$login' ";
                                //Affichage de la selection
                                $result_usr = @mysql_query($query_usr) OR DIE("Erreur de la s&eacute;lection des utilisateurs : " . mysql_error());

                                //Selection de la table
                                $query_user = "SELECT * FROM " . $database . "." . $tb_utilisateurs . " ";
                                //Affichage de la selection
                                $result_user = @mysql_query($query_user) OR DIE("Erreur de la s&eacute;lection des utilisateurs : " . mysql_error());

                                if (@$_POST['message_id']) {
                                    $query_msg = "SELECT * FROM " . $database . "." . $tb_mp . " WHERE id_mp = " . $_POST['message_id'] . "";
                                    //Affichage de la selection
                                    $result_msg = @mysql_query($query_msg) OR DIE("Erreur de la s&eacute;lection des messages : " . mysql_error());
                                    $msg = mysql_fetch_assoc($result_msg);
                                    $date_usa = $msg['date_mp'];
                                    $date_msg = substr($date_usa, 8, 10) . "/" . substr(substr($date_usa, 5, 7), 0, 2) . "/" . substr($date_usa, 0, 4);
                                    $heure_usa = $msg['heure_mp'];
                                    $heure_msg = substr($heure_usa, 0, 2) . "h" . substr($heure_usa, 3, 5);
                                    $query_rep = "SELECT * FROM " . $database . "." . $tb_mp_rep . " WHERE id_mp_rep = " . $_POST['message_id'] . "";
                                    //Affichage de la selection
                                    $result_rep = @mysql_query($query_rep) OR DIE("Erreur de la s&eacute;lection des messages : " . mysql_error());
                                    ?>
                                    <div class="comment-box rounded">
                                        Envoy&eacute; par <b><a href="./user.php?user=<?php echo $msg['from_mp']; ?>" title="Voir le profil de <?php echo $msg['from_mp']; ?>"><?php echo $msg['from_mp']; ?></a></b>, le <i><?php echo $date_msg; ?></i> &agrave; <i><?php echo $heure_msg; ?></i><br />
                                        <br />
                                        <?php echo addChar(utf8_encode($msg['message_mp'])); ?> <br />
                                    </div>
                                    <?php
                                    if (mysql_num_rows($result_rep) == 0) {
                                        
                                    } else {
                                        while ($comment = mysql_fetch_assoc($result_rep)) {
                                            $date_usa = $comment['date_mp_rep'];
                                            $date_rep = substr($date_usa, 8, 10) . "/" . substr(substr($date_usa, 5, 7), 0, 2) . "/" . substr($date_usa, 0, 4);
                                            $heure_usa = $comment['heure_mp_rep'];
                                            $heure_rep = substr($heure_usa, 0, 2) . "h" . substr($heure_usa, 3, 5);
                                            ?>
                                            <div class="comment-box rounded">
                                                Envoy&eacute; par <b><a href="./user.php?user=<?php echo $comment['from_mp_rep']; ?>" title="Voir le profil de <?php echo $comment['from_mp_rep']; ?>"><?php echo $comment['from_mp_rep']; ?></a></b>, le <i><?php echo $date_rep; ?></i> &agrave; <i><?php echo $heure_rep; ?></i><br />
                                                <br />
                                                <?php echo addChar(utf8_encode($comment['message_mp_rep'])); ?> <br />
                                            </div>
                                            <?php
                                        }
                                    }
                                    ?>
                                    <br />
                                    <br />
                                    <br />
                                    <br />
                                    <table>
                                        <tr>
                                            <td class="title_form" colspan="2">R&eacute;pondre au message</td>
                                        </tr>
                                        <tr>
                                            <td class="text_form">Message :</td>
                                            <form method="post" action="./message.php?action=reply">
                                                <td><textarea cols="50" rows="5" name="message_rep">Votre message ici...</textarea></td>
                                        </tr>
                                        <tr>
                                            <td colspan="2" valign="middle" align="center">
                                                <div class="btn_center">

                                                    <input type="hidden" name="message_id_rep" value="<?php echo $_POST['message_id']; ?>" />
                                                    <input type="submit" name="reply" value="R&eacute;pondre" class="btn left" />
                                                    </form>
                                                    <form method="POST" action="./message.php?user=<?php echo $_SESSION['login']; ?>&action=back">
                                                        <input type="submit" name="back_mp_rep" value="Retourner &agrave; la bo&icirc;te de r&eacute;ception" class="btn right" />
                                                    </form>
                                                </div>
                                            </td>
                                        </tr>
                                    </table>
                                    <br />
                                    <?php
                                } else if (@$_GET['action'] == "new") {
                                    if ($_GET['contact']) {
                                        $contact = $_GET['contact'];
                                        ?>
                                        <table>
                                            <form method="post" action="./message.php?action=send&contact=<?php echo $contact; ?>">
                                                <tr>
                                                    <td class="title_form" colspan="2">Envoy&eacute; un message priv&eacute;</td>
                                                </tr>
                                                <tr>
                                                    <td class="text_form">&Agrave; :</td>
                                                    <td><b><a href="./user.php?user=<?php echo $contact; ?>" title="Voir le profil de <?php echo $contact; ?>" name="to_contact"><?php echo $contact; ?></a></b></td>
                                                </tr>
                                                <tr>
                                                    <td class="text_form">Sujet :</td>
                                                    <td><input type="text" name="sujet_contact" /></td>
                                                </tr>
                                                <tr>
                                                    <td class="text_form">Message :</td>
                                                    <td><textarea cols="50" rows="5" name="message_contact">Votre message ici...</textarea></td>
                                                </tr>
                                                <tr>
                                                    <td colspan="2" valign="middle" align="center">
                                                        <input type="submit" name="send_contact" value="Envoyer" title="Envoyez votre message maintenant !" class="btn" />
                                                    </td>
                                                </tr>
                                            </form>
                                        </table>
                                        <?php
                                    } else {
                                        ?>
                                        <table>
                                            <form method="post" action="./message.php?action=send">
                                                <tr>
                                                    <td class="title_form" colspan="2">Envoyer un message priv&eacute;</td>
                                                </tr>
                                                <tr>
                                                    <td class="text_form">&Agrave; :</td>
                                                    <td>
                                                        <select name="to">
                                                            <?php
                                                            while ($user = mysql_fetch_assoc($result_user)) {
                                                                ?>
                                                                <option value="<?php echo $user['login']; ?>"><?php echo $user['login']; ?></option>
                                                                <?php
                                                            }
                                                            ?>
                                                        </select>
                                                    </td>
                                                </tr>
                                                <tr>
                                                    <td class="text_form">Sujet :</td>
                                                    <td><input type="text" name="sujet" /></td>
                                                </tr>
                                                <tr>
                                                    <td class="text_form">Message :</td>
                                                    <td><textarea cols="50" rows="5" name="message">Votre message ici...</textarea></td>
                                                </tr>
                                                <tr>
                                                    <td colspan="2" valign="middle" align="center">
                                                        <input type="submit" name="send" value="Envoyer" title="Envoyez votre message maintenant !" class="btn" />
                                                    </td>
                                                </tr>
                                            </form>
                                        </table>
                                        <?php
                                    }
                                } else if (@$_POST['send']) {
                                    $pseudo = $_POST['to'];
                                    $from = $login;
                                    $to = $_POST['to'];
                                    $title = mysql_escape_string($_POST['sujet']);
                                    $secure_message = mysql_escape_string($_POST['message']);
                                    $message = addChar(utf8_encode($secure_message));
                                    $date = date('Y') . "-" . date('m') . "-" . date('j');
                                    $heure = date('H') . ":" . date('i');

                                    //Selection de la table
                                    $query_usr_add = "INSERT INTO " . $database . "." . $tb_mp . " SET pseudo_mp = '$pseudo', from_mp = '$from', to_mp = '$to', title_mp = '$title', message_mp = '$message', date_mp = '$date', heure_mp = '$heure' ";
                                    //Affichage de la selection
                                    $result_usr_add = @mysql_query($query_usr_add) OR DIE("Erreur de l'ajout du nouveau message : " . mysql_error());
                                    ?>
                                    <div id="box" onclick="document.getElementById('box').style.display='none';" class="success-box rounded" title="Fermer la fen&ecirc;tre">
                                        Votre message a bien &eacute;t&eacute; envoy&eacute;.
                                    </div>
                                    <br />
                                    <br />
                                    <a href="./message.php?user=<?php echo $_SESSION['login']; ?>&action=back" class="btn">Retourner &agrave; la bo&icirc;te de r&eacute;ception</a> <br />
                                    <?php
                                } else if (@$_POST['send_contact']) {
                                    $pseudo_contact = $_GET['contact'];
                                    $from_contact = $login;
                                    $to_contact = $_GET['contact'];
                                    $title_contact = mysql_escape_string($_POST['sujet_contact']);
                                    $secure_message_contact = mysql_escape_string($_POST['message_contact']);
                                    $message_contact = addChar(utf8_encode($secure_message_contact));
                                    $date_contact = date('Y') . "-" . date('m') . "-" . date('j');
                                    $heure_contact = date('H') . ":" . date('i');

                                    //Selection de la table
                                    $query_usr_add_contact = "INSERT INTO " . $database . "." . $tb_mp . " SET pseudo_mp = '$pseudo_contact', from_mp = '$from_contact', to_mp = '$to_contact', title_mp = '$title_contact', message_mp = '$message_contact', date_mp = '$date_contact', heure_mp = '$heure_contact' ";
                                    //Affichage de la selection
                                    $result_usr_add_contact = @mysql_query($query_usr_add_contact) OR DIE("Erreur de l'ajout du nouveau message : " . mysql_error());
                                    ?>
                                    <div id="box" onclick="document.getElementById('box').style.display='none';" class="success-box rounded" title="Fermer la fen&ecirc;tre">
                                        Votre message a bien &eacute;t&eacute; envoy&eacute;.
                                    </div>
                                    <br />
                                    <br />
                                    <a href="./message.php?user=<?php echo $_SESSION['login']; ?>&action=back" class="btn">Retourner &agrave; la bo&icirc;te de r&eacute;ception</a> <br />
                                    <?php
                                } else if (@$_GET['action'] == "reply" || @$_POST['reply']) {
                                    $id_mp_rep = $_POST['message_id_rep'];
                                    $from_rep = $_SESSION['login'];
                                    $secure_message_rep = mysql_escape_string($_POST['message_rep']);
                                    $message_rep = addChar(utf8_encode($secure_message_rep));
                                    $date_rep = date('Y') . "-" . date('m') . "-" . date('j');
                                    $heure_rep = date('H') . ":" . date('i');

                                    //Selection de la table
                                    $query_usr_rep = "INSERT INTO " . $database . "." . $tb_mp_rep . " SET id_mp_rep = '$id_mp_rep', from_mp_rep = '$from_rep', message_mp_rep = '$message_rep', date_mp_rep = '$date_rep', heure_mp_rep = '$heure_rep' ";
                                    //Affichage de la selection
                                    $result_usr_rep = @mysql_query($query_usr_rep) OR DIE("Erreur de l'ajout de la r&eacute;ponse : " . mysql_error());
                                    ?>
                                    <div id="box" onclick="document.getElementById('box').style.display='none';" class="success-box rounded" title="Fermer la fen&ecirc;tre">
                                        Votre message a bien &eacute;t&eacute; envoy&eacute;.
                                    </div>
                                    <br />
                                    <br />
                                    <a href="./message.php?user=<?php echo $_SESSION['login']; ?>&action=back" class="btn">Retourner &agrave; la bo&icirc;te de r&eacute;ception</a> <br />
                                    <?php
                                } else if (@$_GET['action'] == "delete") {
                                    $del_mp = "DELETE FROM " . $database . "." . $tb_mp . " WHERE `tb_mp`.`id_mp` = " . $_POST['del_id'] . "";
                                    $del_mp_result = mysql_query($del_mp) OR DIE("<i>" . "Erreur lors de la supression : " . mysql_error() . "</i>");
                                    ?>
                                    <div id="box" onclick="document.getElementById('box').style.display='none';" class="success-box rounded" title="Fermer la fen&ecirc;tre">
                                        Votre message a bien &eacute;t&eacute; Suprimm&eacute;.
                                    </div>
                                    <br />
                                    <br />
                                    <a href="./message.php?user=<?php echo $_SESSION['login']; ?>&action=back" class="btn">Retourner &agrave; la bo&icirc;te de r&eacute;ception</a> <br />
                                    <?php
                                } else if (!$login) {
                                    ?>
                                    <div id="box" onclick="document.getElementById('box').style.display='none';" class="error-box rounded" title="Fermer la fen&ecirc;tre">
                                        Erreur lors de la s&eacute;l&eacute;ction des messages.
                                    </div>
                                    <br />
                                    <br />
                                    <a href="./index.php?action=back" class="btn">Retourner &agrave; l'accueil</a> <br />
                                    <?php
                                } else {
                                    show_mp_box();
                                }
                                ?>
                            </center>
                            <br />
                        </div>
                        <?php show_footer(); ?>
                    </div>
                </div>
            </div>
        </body>
    </html>
<?php } ?>